package com.genymobile.scrcpy.five.cmd;

import com.genymobile.scrcpy.five.inject.FiveInjector;

import java.lang.reflect.Field;

public class ContextCmd extends ICmd {
    public String name = null;
    public int displayId = Integer.MIN_VALUE;
    public int fingerId = Integer.MIN_VALUE;
    @Override
    public boolean run(FiveInjector injector) {
        return false;
    }

    @Override
    public String toString() {
        return super.toString() +
                "name=" + name +
                " displayId=" + displayId +
                " fingerId=" + fingerId;
    }

    @Override
    ICmd parse(long timestampMs, String[] arr) {
        ContextCmd contextCmd = new ContextCmd();
        for (String line : arr) {
            parseAndSet(line, contextCmd);
        }
        return contextCmd;
    }

    public void apply(ICmd cmd) {
        for (Field f : this.getClass().getFields()) {
            if ("name".equals(f.getName())) {
                continue;
            }
            try {
                Class<?> fieldType = f.getType();
                if (fieldType == int.class || fieldType == Integer.class) {
                    int v = f.getInt(this);
                    if (v != Integer.MIN_VALUE) {
                        setFiled(cmd, cmd.getClass().getField(f.getName()), v);
                    }
                } else if (fieldType == float.class || fieldType == Float.class) {
                    float v = f.getFloat(this);
                    if (v != Float.MIN_VALUE) {
                        setFiled(cmd, cmd.getClass().getField(f.getName()), v);
                    }
                } else if (fieldType == double.class || fieldType == Double.class) {
                    double v = f.getDouble(this);
                    if (v != Double.MIN_VALUE) {
                        setFiled(cmd, cmd.getClass().getField(f.getName()), v);
                    }
                }
            } catch (Exception ignored) {
            }
        }
    }

    @Override
    String getCmdName() {
        return "context";
    }
}
